﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.Entity.AutoGenerator;
using DataFrameworkAccess;
using Xinkui.Common;

namespace Xinkui.DataAccess.AutoGenerator {
    public partial class ProductInfoDataAccess {

        public List<ProductInfo_Entity> ListShowRoomProducts(int pageIndex, int pageSize, int companyId, int cateId, out int total) {
            total = 0;
            try {
                int tempRecord = 0;
                if (pageIndex <= 0) pageIndex = 1;
                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_ShowRoomListByPaging");
                cmd.SetParameterValue("@where", WhereHelper.Product.CreateShowRoomProductListWhere(companyId, cateId));
                cmd.SetParameterValue("@PageSize", pageSize);
                cmd.SetParameterValue("@PageIndex", pageIndex);
                cmd.SetParameterValue("@RecordCount", tempRecord);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                object cmdGetParameterValue = cmd.GetParameterValue("@RecordCount");
                if (!(cmdGetParameterValue is DBNull)) {
                    total = Convert.ToInt32(cmdGetParameterValue);
                }
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);
        }

        public List<ProductInfo_Entity> ListShowRoomFeaturedProducts(int pageSize, int companyId) {

            try {

                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_ShowRoomFeaturedList");

                cmd.SetParameterValue("@PageSize", pageSize);

                cmd.SetParameterValue("@CompanyId", companyId);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);
        }


        public List<ProductInfo_Entity> ListHomeRecommendProducts(int pageSize, int categoryId) {

            try {

                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_HomeRecommendList");

                cmd.SetParameterValue("@PageSize", pageSize);
                cmd.SetParameterValue("@CategoryID", categoryId);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);
        }

        public List<ProductInfo_Entity> ListLastProduct(int pageSize) {
            try {

                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_LastList");
                cmd.SetParameterValue("@PageSize", pageSize);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);
        }


        public List<ProductInfo_Entity> SearchShowRoomProducts(string keyword, int pageIndex, int pageSize, int companyId, int cateId, out int total) {
            total = 0;
            try {
                int tempRecord = 0;
                if (pageIndex <= 0) pageIndex = 1;
                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_ShowRoomListByPaging");
                cmd.SetParameterValue("@where", WhereHelper.Product.CreateShowRoomProductSearchWhere(keyword, companyId, cateId));
                cmd.SetParameterValue("@PageSize", pageSize);
                cmd.SetParameterValue("@PageIndex", pageIndex);
                cmd.SetParameterValue("@RecordCount", tempRecord);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                object cmdGetParameterValue = cmd.GetParameterValue("@RecordCount");
                if (!(cmdGetParameterValue is DBNull)) {
                    total = Convert.ToInt32(cmdGetParameterValue);
                }
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);
        }


        public List<ProductInfo_Entity> ListProductsByCategory(int pageIndex, int pageSize, int cateId, out int total) {
            total = 0;
            try {
                int tempRecord = 0;
                if (pageIndex <= 0) pageIndex = 1;
                DataCommand cmd = DataCommandManager.GetDataCommand("ProductInfo_ShowRoomListByPaging");
                cmd.SetParameterValue("@where", WhereHelper.Product.CreateCatalogWhere(cateId));
                cmd.SetParameterValue("@PageSize", pageSize);
                cmd.SetParameterValue("@PageIndex", pageIndex);
                cmd.SetParameterValue("@RecordCount", tempRecord);
                List<ProductInfo_Entity> pageList = cmd.ExecuteEntityList<ProductInfo_Entity>();
                object cmdGetParameterValue = cmd.GetParameterValue("@RecordCount");
                if (!(cmdGetParameterValue is DBNull)) {
                    total = Convert.ToInt32(cmdGetParameterValue);
                }
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfo_Entity>(0);

        }

        public List<ProductInfoMaintain_Entity> UserCenter_ManagerListProduct(int pageIndex, int pageSize, int companyId, int auditstatus, out int total) {
            //Query.UserCenter.ManagerListProduct
            total = 0;
            try {
                int tempRecord = 0;
                if (pageIndex <= 0) pageIndex = 1;
                DataCommand cmd = DataCommandManager.GetDataCommand("Query.UserCenter.ManagerListProduct");
                cmd.SetParameterValue("@AuditStatus", auditstatus);
                cmd.SetParameterValue("@CompanyId", companyId);
                cmd.SetParameterValue("@PageSize", pageSize);
                cmd.SetParameterValue("@PageIndex", pageIndex);
                cmd.SetParameterValue("@RecordCount", tempRecord);
                List<ProductInfoMaintain_Entity> pageList = cmd.ExecuteEntityList<ProductInfoMaintain_Entity>();
                object cmdGetParameterValue = cmd.GetParameterValue("@RecordCount");
                if (!(cmdGetParameterValue is DBNull)) {
                    total = Convert.ToInt32(cmdGetParameterValue);
                }
                return pageList;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<ProductInfoMaintain_Entity>(0);
        }



        public void ProductRepost(int productId) {
            //Query.UserCenter.ManagerListProduct

            try {

                DataCommand cmd = DataCommandManager.GetDataCommand("Product.Operation.Repost");
                cmd.SetParameterValue("@ProductId", productId);

                cmd.ExecuteNonQuery();

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }


        }


        public void ProductDel(int productId) {
            //Query.UserCenter.ManagerListProduct

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("Product.Operation.Del");
                cmd.SetParameterValue("@ProductId", productId);

                cmd.ExecuteNonQuery();

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }


        }





    }


    public static partial class WhereHelper {
        public class Product {
            public static string CreateShowRoomProductListWhere(int companyId, int categoryId) {
                string where = string.Format(" A.CompanyID = {0}", companyId);
                if (categoryId > 0)
                    where = where.AndLike("B.DirPath", string.Format("'%/{0}/%'", categoryId));
                return where;
            }

            public static string CreateSearchWhere(string keyword) {
                string where = string.Empty;
                keyword = SQLHelper.MyEncodeInputString(keyword);
                if (!string.IsNullOrEmpty(keyword) && keyword.Length > 0) {

                    where = string.Format(@" FREETEXT(A.ProductName,'{0}')
or FREETEXT(A.ProductDescription,'{0}')
or FREETEXT(A.Keywords,'{0}')
or FREETEXT(A.MainKeyword,'{0}')
", keyword);

                }

                return where;
            }


            public static string CreateShowRoomProductSearchWhere(string key, int companyId, int categoryId) {
                string where = string.Format(" A.CompanyID = {0}", companyId);
                if (categoryId > 0)
                    where = where.AndLike("B.DirPath", string.Format("'%/{0}/%'", categoryId));
                if (!string.IsNullOrEmpty(key)) {
                    where = where.AndLike("A.ProductName", string.Format("'%{0}%'", SQLHelper.MyEncodeInputString(key)));
                }
                return where;
            }
            public static string CreateCatalogWhere(int categoryId) {
                string where = " 1=1";
                if (categoryId > 0)
                    where = where.AndLike("B.DirPath", string.Format("'%/{0}/%'", categoryId));
                return where;
            }
        }

        public class Company {

            public static string CreateCatalogWhere(int categoryId) {
                string where = " 1=1";
                if (categoryId > 0)
                    where = where.AndLike("B.DirPath", string.Format("'%/{0}/%'", categoryId));
                return where;
            }


            public static string CreateSearchWhere(string keyword) {
                string where = string.Empty;
                keyword = SQLHelper.MyEncodeInputString(keyword);
                if (!string.IsNullOrEmpty(keyword) && keyword.Length > 0) {

                    where = string.Format(@" FREETEXT(A.MainProduct,'{0}')
or FREETEXT(A.CompanyName,'{0}')
or FREETEXT(A.KeyWord,'{0}')
or FREETEXT(A.CompanyDescription,'{0}')
", keyword);

                }

                return where;
            }
        }



        public static string AndLike(this string where, string key, string value) {
            return string.Format("{0} and {1} like {2}", where, key, value);
        }

        public static string OrLike(this string where, string key, string value) {
            return string.Format("{0} or {1} like {2}", where, key, value);
        }

        public static string And(this string where, string key, string value) {
            return string.Format("{0} and {1}={2}", where, key, value);
        }

        public static string Or(this string where, string key, string value) {
            return string.Format("{0} or {1}={2}", where, key, value);
        }




    }
}
